package com.atguigu.eduorder.controller;


import com.atguigu.common.R;
import com.atguigu.common.jwt.JwtUtils;
import com.atguigu.eduorder.entity.TOrder;
import com.atguigu.eduorder.service.TOrderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author Panghl
 * @since 2021-02-21
 */
@RestController
@RequestMapping("/eduorder/t-order")
@Api(tags = {"订单管理"})
public class TOrderController {

    @Autowired
    private TOrderService orderService;

    @ApiOperation("生成订单方法")
    @PostMapping("/createOrder/{courseId}")
    public R saveOrder(@PathVariable String courseId, HttpServletRequest request) {
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //生成订单号
        String orderNo = orderService.createOrder(courseId, memberId);
        return R.ok().data("orderNo", orderNo);
    }

    @ApiOperation("根据订单id查询订单信息")
    @GetMapping("/getOrdersInfo/{orderNo}")
    public R getOrdersInfo(@PathVariable("orderNo") String orderNo) {
        TOrder order = orderService.getByOrderNo(orderNo);
        return R.ok().data("order", order);
    }

    @ApiOperation("是否购买课程")
    @GetMapping("/isBuyCourse/{memberId}/{courseId}")
    public boolean isBuyCourse(@PathVariable("memberId") String memberId,
                               @PathVariable("courseId") String courseId) {
        QueryWrapper<TOrder> qw = new QueryWrapper<TOrder>();
        qw.eq("member_id", memberId);
        qw.eq("course_id", courseId);
        qw.eq("status", 1);
        int count = orderService.count(qw);
        if (count >= 1) {
            return true;
        } else {
            return false;
        }
    }

}
